/*******************************************************************************
 * @file LockingPtr.h
 * Definition of class template for LockingPtr<>
 * 
 * @author mk
 * @date 28/12/2008 13:50:21
 * @version 0.1
 *
 * The following class is a copy of the Article taken from
 * http://www.ddj.com/cpp/184403766
 * with some minor enhancements.
 ******************************************************************************/

#ifndef __LOCKING_PTR_H_INCLUDED__
#define __LOCKING_PTR_H_INCLUDED__

namespace boost
{
	class mutex;
}

template <typename T>
class LockingPtr 
{
public:
	LockingPtr(volatile T& obj, boost::mutex& mtx) : 
		pObj_(const_cast<T*>(&obj)), pMtx_(&mtx)
	{
		pMtx_->lock();
	}

	LockingPtr(volatile T& obj, volatile boost::mutex& mtx) : 
		pObj_(const_cast<T*>(&obj)), pMtx_(const_cast<boost::mutex*>(&mtx))
	{
		pMtx_->lock();
	}

	~LockingPtr()
	{
		pMtx_->unlock();
	}
	
	T& operator*()
	{
		return *pObj_;
	}
	
	T* operator->()
	{
		return pObj_;
	}

private:
	LockingPtr(const LockingPtr&);
	LockingPtr& operator=(const LockingPtr&);
	T* pObj_;
	boost::mutex* pMtx_;
};

#endif /* __LOCKING_PTR_H_INCLUDED__ */